package com.kehutong.common.util;

import com.kehutong.common.DeptService;
import com.kehutong.common.DoveClient;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.util.Ioc;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.List;

/**
 * 企微端小区数据权限
 *
 * @author: liukunlong
 * @date: 2021-08-17 21:03
 */
public class AppDataPvUtils {
    private static final Logger logger = LoggerFactory.getLogger(AppDataPvUtils.class);

    /**
     * 获取App数据权限
     * 1.全部数据返回
     *
     * @param token
     * @return
     * @throws Exception
     */
    public static JSONArray getAppCommunityDataPv(Token token, JSONObject jsonObject) throws Exception {
        JSONArray result = new JSONArray();
        String communityNo = jsonObject.getString("communityNo");
        jsonObject.remove("communityNo");



        //小区为空,查询小区数据权限
        if (Objects.isEmpty(communityNo)) {
            //如果是管理员查询全部
            if (token.getRoles().contains("5") ) {
                return result;
            }
            //非管理员查询负责的小区
            DoveClient doveClient = Ioc.getInstance(DoveClient.class);
            JSONObject resultCommunity = doveClient.post("/basic/community/service/list", (http) -> {
                http.addParam("employee_ids", token.getUuid());
            });
            JSONArray communityArray = resultCommunity.getJSONArray("data");
            if (Objects.nonEmpty(communityArray)) {
                for (int i = 0; i < communityArray.size(); i++) {
                    JSONObject communityJson = communityArray.getJSONObject(i);
                    result.add(communityJson.getString("id"));
                }
            } else {
                result.add("-1");
            }
            return result;
        }

        //传入小区则根据小区查询
        result.add(communityNo);
        return result;


    }
}
